﻿using S7200SmartDll;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HardWare

{
    
    class PLC : HardWare
    {
        private S7200SmartClient plc = null;

        private String ip ;
        private int rack ;
        private int slot ;
        private ushort connectionType ;



        public void init(Dictionary<string, string> config)
        {
            ip = config["ip"];
            rack = int.Parse(config["rack"]);
            slot = int.Parse(config["slot"]);
            connectionType = ushort.Parse(config["connectionType"]);
            if (plc != null)
            {
                plc = new S7200SmartClient();
                plc.SetConnectionType(connectionType);
            }  

        }

        private Boolean isConnected = false;

        public bool IsConnected { get => isConnected; set => isConnected = value; }

        public int Open()
        {
            if (isConnected)
            {
                return 0;
            }

            int ret = plc.ConnectTo(ip, rack, slot, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            if (ret == 0)
            {
                // 连接成功
                isConnected = true;

            }
            else
            {
                isConnected = false;
            }
            return ret;
        }

        public void Close()
        {
            if (isConnected)
            {
                plc.Disconnect();
                IsConnected = false;
            }
        }

        private float readVData(int start)
        {
            
            return plc.VRead_REAL(start);
        }

        public object read(string path)
        {
            String[] arr = path.Split
            int count = path.Split(',').Length;
            if(cou)
        }

    }
}
